package com.ys.peony.ui.activity;

import android.content.Intent;
import android.view.View;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ys.peony.R;
import com.ys.peony.app.YSConstant;
import com.ys.peony.base.BaseActivity;
import com.ys.peony.model.AreaBean;
import com.ys.peony.model.CarBrachBean;
import com.ys.peony.model.CarBrandModel;
import com.ys.peony.model.YWDJPrepareAreaItemModel;
import com.ys.peony.ui.adapter.SimpleTreeAdapter;
import com.ys.peony.utils.JSONUtil;
import com.ys.peony.utils.SharePreferencesUtils;
import com.ys.peony.utils.StringUtils;
import com.zhy.tree.bean.Node;
import com.zhy.tree.bean.TreeListViewAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Created by yml on 17/6/13.
 */

public class CarBranchPickerActivity extends BaseActivity implements View.OnClickListener{

    @BindView(R.id.back_btn)
    RelativeLayout backBtn;

    @BindView(R.id.title_name_tv)
    TextView titleNameTv;

    @BindView(R.id.option_lv)
    ListView lv;

    private List<CarBrandModel> options;
    private TreeListViewAdapter adapter;

    private List<CarBrachBean> mDatas = new ArrayList<CarBrachBean>();

    @Override
    protected int getContentResId() {
        return R.layout.picker_layout;
    }

    @Override
    protected void initView() {
        titleNameTv.setText(getResources().getString(R.string.select_carbrand_title));
        backBtn.setOnClickListener(this);

        //options初始化
        String brachStr=SharePreferencesUtils.getInstance().getValueByKey(YSConstant.SP_CAR_BRANCH);
        if(!StringUtils.isEmpty(brachStr)){
            options= (List<CarBrandModel>) JSONUtil.parseArray(brachStr,CarBrandModel.class);
        }

        setDates(options);
        try {
            adapter = new SimpleTreeAdapter<CarBrachBean>(lv, this, mDatas, 10);

            adapter.setOnTreeNodeClickListener(new TreeListViewAdapter.OnTreeNodeClickListener() {
                @Override
                public void onClick(Node node, int position) {
                    if (node.isLeaf()) {
                        //TODO 设置返回参数
                        Intent it=new Intent();
                        it.putExtra("name",node.getName());
                        it.putExtra("id",node.getId());
                        setResult(RESULT_OK,it);
                        finish();
                    }
                }

            });

            lv.setAdapter(adapter);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }
    private void setDates(List<CarBrandModel> options) {
        mDatas.clear();
        setChildDates(options, 0);
    }


    private void setChildDates(List<CarBrandModel> options, int pid) {
        for (int i = 0; i < options.size(); i++) {
            mDatas.add(new CarBrachBean((int) options.get(i).getId(), pid, options.get(i).getName(), options.get(i)));
            setChildDates(options.get(i).getBusiBrandList(), (int) options.get(i).getId());
        }
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.back_btn:
                finish();
                break;
        }
    }
}
